package product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.yaml.snakeyaml.events.Event;
import product.domain.*;
import product.mapper.AttrAttrgroupRelationMapper;
import product.mapper.AttrMapper;
import product.mapper.CategoryMapper;
import product.service.AttrGroupService;
import product.mapper.AttrGroupMapper;
import org.springframework.stereotype.Service;
import product.service.AttrService;
import product.utils.GetUtils;
import product.vo.AttrGroupWithAttrsVo;
import product.vo.AttrRelationVo;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author panghu
 * @description 针对表【pms_attr_group(属性分组)】的数据库操作Service实现
 * @createDate 2023-03-04 21:03:08
 */
@Service
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroup>
        implements AttrGroupService {

    @Autowired
    private GetUtils getUtils;

    @Autowired
    private AttrMapper attrMapper;

    @Autowired
    private AttrAttrgroupRelationMapper attrAttrgroupRelationMapper;

    @Autowired
    private AttrService attrService;

    @Override
    public Page<AttrGroup> queryPage(Map<String, Object> params, Long catId) {
        Page<AttrGroup> page = new Page<>(Integer.parseInt((String) params.get("page")), Integer.parseInt((String) params.get("limit")));
        LambdaQueryWrapper<AttrGroup> queryWrapper;
        if (catId == 0) {
            // 查询全部
            queryWrapper = getQueryWrapper(params);
        } else {
            // 查询指定分类下的数据
            queryWrapper = getQueryWrapper(params).eq(AttrGroup::getCatelogId, catId);
        }
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public AttrGroup getAttrGroupById(Long attrGroupId) {
        List<Long> paths = new ArrayList<>();
        AttrGroup attrGroup = this.getById(attrGroupId);
        List<Long> catelogPath = getUtils.getCategoryIdPath(attrGroup.getCatelogId(), paths);
        Collections.reverse(catelogPath);
        attrGroup.setCatelogPath(catelogPath.toArray(new Long[0]));
        return attrGroup;
    }

    @Override
    public List<Attr> getAttrRelation(Long attrGroupId) {
        List<AttrAttrgroupRelation> attrAttrgroupRelationList = attrAttrgroupRelationMapper.selectList(new LambdaQueryWrapper<AttrAttrgroupRelation>().eq(AttrAttrgroupRelation::getAttrGroupId, attrGroupId));
        return attrAttrgroupRelationList.stream().map(attrAttrgroupRelation ->
                attrMapper.selectById(attrAttrgroupRelation.getAttrId())
        ).collect(Collectors.toList());
    }

    @Override
    public boolean removeAttrRelation(AttrRelationVo[] attrRelationVos) {
        List<AttrAttrgroupRelation> relationList = Arrays.asList(attrRelationVos).stream().map(attrRelationVo -> {
            // 转成relatuon
            AttrAttrgroupRelation relation = new AttrAttrgroupRelation();
            BeanUtils.copyProperties(attrRelationVo, relation);
            return relation;
        }).collect(Collectors.toList());
        // 批量删除
        attrAttrgroupRelationMapper.deleteBatchRelation(relationList);
        return true;
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatId(Long catId) {
        // 1、根据分类Id查找属性分组
        List<AttrGroup> attrGroups = this.baseMapper.selectList(new LambdaQueryWrapper<AttrGroup>().eq(AttrGroup::getCatelogId, catId));
        return attrGroups.stream().map(attrGroup -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(attrGroup, attrGroupWithAttrsVo);
            // 2、查询该属性分组下的属性
            List<Attr> attrList = attrService.getRelationAttr(attrGroup.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrList);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
    }


    @Override
    public Page<AttrGroup> queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<AttrGroup> queryWrapper = getQueryWrapper(params);
        Page<AttrGroup> page = new Page<>(Integer.parseInt((String) params.get("page")), Integer.parseInt((String) params.get("limit")));
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public LambdaQueryWrapper<AttrGroup> getQueryWrapper(Map<String, Object> params) {
        LambdaQueryWrapper<AttrGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(params.containsKey("key"), AttrGroup::getAttrGroupId, params.get("key")).or().like(params.containsKey("key"), AttrGroup::getAttrGroupName, params.get("key"));
        return wrapper;
    }


}




